package com.ershui.blog.article.controller;

import com.ershui.blog.article.domain.LDiary;
import com.ershui.blog.article.service.LDiaryService;
import com.ershui.blog.system.controller.BaseController;
import com.ershui.blog.system.domain.AjaxResult;
import com.ershui.blog.system.domain.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/diary")
public class DiaryController extends BaseController {
    @Autowired
    private LDiaryService diaryService;


    @PostMapping("/add")
    public AjaxResult AddDiary(LDiary diary){
        try {
            diaryService.insert(diary);
            return AjaxResult.sucess();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @GetMapping("/select")
    public TableDataInfo GetDiary(){
        startPage();
        List<LDiary> list=diaryService.selectDiaryList();
        return getDataTable(list);
    }

    @PutMapping("/update")
    @ResponseBody
    public AjaxResult UpdateDiary(@RequestBody LDiary diary){
        try {
            diaryService.updateDiary(diary);
            return AjaxResult.sucess();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @DeleteMapping("/delete")
    public AjaxResult DeleteDiaryById(Long id){
        try{
            diaryService.deleteDiaryById(id);
            return  AjaxResult.sucess();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
}
